using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace CDTM.Modules.TraCuuDiem.Components
{
    public partial class TuyenSinh
    {
        #region Properties.

        public int SBD { set; get; }
        public string BTS { set; get; }
        public string DonVi { set; get; }
        public string MHS { set; get; }
        public string HoTenLot { set; get; }
        public string Ten { set; get; }
        public DateTime NgaySinh { set; get; }
        public bool GioiTinh { set; get; }
        public string Tinh { set; get; }
        public string Huyen { set; get; }
        public string HoKhau { set; get; }
        public string DoiTuong { set; get; }
        public int UuTien { set; get; }
        public string KhuVuc { set; get; }
        public double Mon1 { set; get; }
        public double Mon2 { set; get; }
        public double Mon3 { set; get; }
        public double Tong { set; get; }
        public string ChuyenNganh { set; get; }
        public string GhiChu { set; get; }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static TuyenSinh Load(int sBD)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_Load]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SBD", SqlDbType.Int, sBD);
            TuyenSinh entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new TuyenSinh();
                if (!reader.IsDBNull(reader.GetOrdinal("SBD"))) entity.SBD = reader.GetInt32(reader.GetOrdinal("SBD"));
                if (!reader.IsDBNull(reader.GetOrdinal("BTS"))) entity.BTS = reader.GetString(reader.GetOrdinal("BTS"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonVi"))) entity.DonVi = reader.GetString(reader.GetOrdinal("DonVi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MHS"))) entity.MHS = reader.GetString(reader.GetOrdinal("MHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetBoolean(reader.GetOrdinal("GioiTinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tinh"))) entity.Tinh = reader.GetString(reader.GetOrdinal("Tinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("Huyen"))) entity.Huyen = reader.GetString(reader.GetOrdinal("Huyen"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoKhau"))) entity.HoKhau = reader.GetString(reader.GetOrdinal("HoKhau"));
                if (!reader.IsDBNull(reader.GetOrdinal("DoiTuong"))) entity.DoiTuong = reader.GetString(reader.GetOrdinal("DoiTuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("UuTien"))) entity.UuTien = reader.GetInt32(reader.GetOrdinal("UuTien"));
                if (!reader.IsDBNull(reader.GetOrdinal("KhuVuc"))) entity.KhuVuc = reader.GetString(reader.GetOrdinal("KhuVuc"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon1"))) entity.Mon1 = reader.GetDouble(reader.GetOrdinal("Mon1"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon2"))) entity.Mon2 = reader.GetDouble(reader.GetOrdinal("Mon2"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon3"))) entity.Mon3 = reader.GetDouble(reader.GetOrdinal("Mon3"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tong"))) entity.Tong = reader.GetDouble(reader.GetOrdinal("Tong"));
                if (!reader.IsDBNull(reader.GetOrdinal("ChuyenNganh"))) entity.ChuyenNganh = reader.GetString(reader.GetOrdinal("ChuyenNganh"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
            }
            reader.Close();
            return entity;
        }

        //---------------------------------------------------------------------------------------------
        public static List<TuyenSinh> SelectCollectionAll()
        {
            List<TuyenSinh> collection = new List<TuyenSinh>();
            SqlDataReader reader = (SqlDataReader)SelectReaderAll();
            while (reader.Read())
            {
                TuyenSinh entity = new TuyenSinh();

                if (!reader.IsDBNull(reader.GetOrdinal("SBD"))) entity.SBD = reader.GetInt32(reader.GetOrdinal("SBD"));
                if (!reader.IsDBNull(reader.GetOrdinal("BTS"))) entity.BTS = reader.GetString(reader.GetOrdinal("BTS"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonVi"))) entity.DonVi = reader.GetString(reader.GetOrdinal("DonVi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MHS"))) entity.MHS = reader.GetString(reader.GetOrdinal("MHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetBoolean(reader.GetOrdinal("GioiTinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tinh"))) entity.Tinh = reader.GetString(reader.GetOrdinal("Tinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("Huyen"))) entity.Huyen = reader.GetString(reader.GetOrdinal("Huyen"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoKhau"))) entity.HoKhau = reader.GetString(reader.GetOrdinal("HoKhau"));
                if (!reader.IsDBNull(reader.GetOrdinal("DoiTuong"))) entity.DoiTuong = reader.GetString(reader.GetOrdinal("DoiTuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("UuTien"))) entity.UuTien = reader.GetInt32(reader.GetOrdinal("UuTien"));
                if (!reader.IsDBNull(reader.GetOrdinal("KhuVuc"))) entity.KhuVuc = reader.GetString(reader.GetOrdinal("KhuVuc"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon1"))) entity.Mon1 = reader.GetDouble(reader.GetOrdinal("Mon1"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon2"))) entity.Mon2 = reader.GetDouble(reader.GetOrdinal("Mon2"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon3"))) entity.Mon3 = reader.GetDouble(reader.GetOrdinal("Mon3"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tong"))) entity.Tong = reader.GetDouble(reader.GetOrdinal("Tong"));
                if (!reader.IsDBNull(reader.GetOrdinal("ChuyenNganh"))) entity.ChuyenNganh = reader.GetString(reader.GetOrdinal("ChuyenNganh"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        public static List<TuyenSinh> SelectCollectionDynamic(string whereCondition, string orderByExpression)
        {
            List<TuyenSinh> collection = new List<TuyenSinh>();

            SqlDataReader reader = (SqlDataReader)SelectReaderDynamic(whereCondition, orderByExpression);
            while (reader.Read())
            {
                TuyenSinh entity = new TuyenSinh();

                if (!reader.IsDBNull(reader.GetOrdinal("SBD"))) entity.SBD = reader.GetInt32(reader.GetOrdinal("SBD"));
                if (!reader.IsDBNull(reader.GetOrdinal("BTS"))) entity.BTS = reader.GetString(reader.GetOrdinal("BTS"));
                if (!reader.IsDBNull(reader.GetOrdinal("DonVi"))) entity.DonVi = reader.GetString(reader.GetOrdinal("DonVi"));
                if (!reader.IsDBNull(reader.GetOrdinal("MHS"))) entity.MHS = reader.GetString(reader.GetOrdinal("MHS"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoTenLot"))) entity.HoTenLot = reader.GetString(reader.GetOrdinal("HoTenLot"));
                if (!reader.IsDBNull(reader.GetOrdinal("Ten"))) entity.Ten = reader.GetString(reader.GetOrdinal("Ten"));
                if (!reader.IsDBNull(reader.GetOrdinal("NgaySinh"))) entity.NgaySinh = reader.GetDateTime(reader.GetOrdinal("NgaySinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("GioiTinh"))) entity.GioiTinh = reader.GetBoolean(reader.GetOrdinal("GioiTinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tinh"))) entity.Tinh = reader.GetString(reader.GetOrdinal("Tinh"));
                if (!reader.IsDBNull(reader.GetOrdinal("Huyen"))) entity.Huyen = reader.GetString(reader.GetOrdinal("Huyen"));
                if (!reader.IsDBNull(reader.GetOrdinal("HoKhau"))) entity.HoKhau = reader.GetString(reader.GetOrdinal("HoKhau"));
                if (!reader.IsDBNull(reader.GetOrdinal("DoiTuong"))) entity.DoiTuong = reader.GetString(reader.GetOrdinal("DoiTuong"));
                if (!reader.IsDBNull(reader.GetOrdinal("UuTien"))) entity.UuTien = reader.GetInt32(reader.GetOrdinal("UuTien"));
                if (!reader.IsDBNull(reader.GetOrdinal("KhuVuc"))) entity.KhuVuc = reader.GetString(reader.GetOrdinal("KhuVuc"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon1"))) entity.Mon1 = reader.GetDouble(reader.GetOrdinal("Mon1"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon2"))) entity.Mon2 = reader.GetDouble(reader.GetOrdinal("Mon2"));
                if (!reader.IsDBNull(reader.GetOrdinal("Mon3"))) entity.Mon3 = reader.GetDouble(reader.GetOrdinal("Mon3"));
                if (!reader.IsDBNull(reader.GetOrdinal("Tong"))) entity.Tong = reader.GetDouble(reader.GetOrdinal("Tong"));
                if (!reader.IsDBNull(reader.GetOrdinal("ChuyenNganh"))) entity.ChuyenNganh = reader.GetString(reader.GetOrdinal("ChuyenNganh"));
                if (!reader.IsDBNull(reader.GetOrdinal("GhiChu"))) entity.GhiChu = reader.GetString(reader.GetOrdinal("GhiChu"));
                collection.Add(entity);
            }

            reader.Close();
            return collection;
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		


        public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_SelectAll]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_SelectDynamic]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        // Select by foreign key return collection		

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int InsertTuyenSinh(string bTS, string donVi, string mHS, string hoTenLot, string ten, DateTime ngaySinh, bool gioiTinh, string tinh, string huyen, string hoKhau, string doiTuong, int uuTien, string khuVuc, double mon1, double mon2, double mon3, double tong, string chuyenNganh, string ghiChu)
        {
            TuyenSinh entity = new TuyenSinh();
            entity.BTS = bTS;
            entity.DonVi = donVi;
            entity.MHS = mHS;
            entity.HoTenLot = hoTenLot;
            entity.Ten = ten;
            entity.NgaySinh = ngaySinh;
            entity.GioiTinh = gioiTinh;
            entity.Tinh = tinh;
            entity.Huyen = huyen;
            entity.HoKhau = hoKhau;
            entity.DoiTuong = doiTuong;
            entity.UuTien = uuTien;
            entity.KhuVuc = khuVuc;
            entity.Mon1 = mon1;
            entity.Mon2 = mon2;
            entity.Mon3 = mon3;
            entity.Tong = tong;
            entity.ChuyenNganh = chuyenNganh;
            entity.GhiChu = ghiChu;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_Insert]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SBD", SqlDbType.Int, SBD);
            db.AddInParameter(dbCommand, "@BTS", SqlDbType.NVarChar, BTS);
            db.AddInParameter(dbCommand, "@DonVi", SqlDbType.NChar, DonVi);
            db.AddInParameter(dbCommand, "@MHS", SqlDbType.NChar, MHS);
            db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
            db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
            db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year < 1753 ? NgaySinh : (object)NgaySinh);
            db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Bit, GioiTinh);
            db.AddInParameter(dbCommand, "@Tinh", SqlDbType.NVarChar, Tinh);
            db.AddInParameter(dbCommand, "@Huyen", SqlDbType.NVarChar, Huyen);
            db.AddInParameter(dbCommand, "@HoKhau", SqlDbType.NVarChar, HoKhau);
            db.AddInParameter(dbCommand, "@DoiTuong", SqlDbType.NChar, DoiTuong);
            db.AddInParameter(dbCommand, "@UuTien", SqlDbType.Int, UuTien);
            db.AddInParameter(dbCommand, "@KhuVuc", SqlDbType.NVarChar, KhuVuc);
            db.AddInParameter(dbCommand, "@Mon1", SqlDbType.Float, Mon1);
            db.AddInParameter(dbCommand, "@Mon2", SqlDbType.Float, Mon2);
            db.AddInParameter(dbCommand, "@Mon3", SqlDbType.Float, Mon3);
            db.AddInParameter(dbCommand, "@Tong", SqlDbType.Float, Tong);
            db.AddInParameter(dbCommand, "@ChuyenNganh", SqlDbType.NVarChar, ChuyenNganh);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                SBD = (int)db.GetParameterValue(dbCommand, "@SBD");
                return SBD;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                SBD = (int)db.GetParameterValue(dbCommand, "@SBD");
                return SBD;
            }
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertCollection(IList<TuyenSinh> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (TuyenSinh item in collection)
                        {
                            if (item.Insert(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert / Update methods.

        public static int InsertUpdateTuyenSinh(int sBD, string bTS, string donVi, string mHS, string hoTenLot, string ten, DateTime ngaySinh, bool gioiTinh, string tinh, string huyen, string hoKhau, string doiTuong, int uuTien, string khuVuc, double mon1, double mon2, double mon3, double tong, string chuyenNganh, string ghiChu)
        {
            TuyenSinh entity = new TuyenSinh();
            entity.SBD = sBD;
            entity.BTS = bTS;
            entity.DonVi = donVi;
            entity.MHS = mHS;
            entity.HoTenLot = hoTenLot;
            entity.Ten = ten;
            entity.NgaySinh = ngaySinh;
            entity.GioiTinh = gioiTinh;
            entity.Tinh = tinh;
            entity.Huyen = huyen;
            entity.HoKhau = hoKhau;
            entity.DoiTuong = doiTuong;
            entity.UuTien = uuTien;
            entity.KhuVuc = khuVuc;
            entity.Mon1 = mon1;
            entity.Mon2 = mon2;
            entity.Mon3 = mon3;
            entity.Tong = tong;
            entity.ChuyenNganh = chuyenNganh;
            entity.GhiChu = ghiChu;
            return entity.InsertUpdate();
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate()
        {
            return this.InsertUpdate(null);
        }

        //---------------------------------------------------------------------------------------------

        public int InsertUpdate(SqlTransaction transaction)
        {
            const string spName = "p_CDTM_TCD_TuyenSinh_InsertUpdate";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SBD", SqlDbType.Int, SBD);
            db.AddInParameter(dbCommand, "@BTS", SqlDbType.NVarChar, BTS);
            db.AddInParameter(dbCommand, "@DonVi", SqlDbType.NChar, DonVi);
            db.AddInParameter(dbCommand, "@MHS", SqlDbType.NChar, MHS);
            db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
            db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
            db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year < 1753 ? NgaySinh : (object)NgaySinh);
            db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Bit, GioiTinh);
            db.AddInParameter(dbCommand, "@Tinh", SqlDbType.NVarChar, Tinh);
            db.AddInParameter(dbCommand, "@Huyen", SqlDbType.NVarChar, Huyen);
            db.AddInParameter(dbCommand, "@HoKhau", SqlDbType.NVarChar, HoKhau);
            db.AddInParameter(dbCommand, "@DoiTuong", SqlDbType.NChar, DoiTuong);
            db.AddInParameter(dbCommand, "@UuTien", SqlDbType.Int, UuTien);
            db.AddInParameter(dbCommand, "@KhuVuc", SqlDbType.NVarChar, KhuVuc);
            db.AddInParameter(dbCommand, "@Mon1", SqlDbType.Float, Mon1);
            db.AddInParameter(dbCommand, "@Mon2", SqlDbType.Float, Mon2);
            db.AddInParameter(dbCommand, "@Mon3", SqlDbType.Float, Mon3);
            db.AddInParameter(dbCommand, "@Tong", SqlDbType.Float, Tong);
            db.AddInParameter(dbCommand, "@ChuyenNganh", SqlDbType.NVarChar, ChuyenNganh);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool InsertUpdateCollection(IList<TuyenSinh> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (TuyenSinh item in collection)
                        {
                            if (item.InsertUpdate(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);

                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int UpdateTuyenSinh(int sBD, string bTS, string donVi, string mHS, string hoTenLot, string ten, DateTime ngaySinh, bool gioiTinh, string tinh, string huyen, string hoKhau, string doiTuong, int uuTien, string khuVuc, double mon1, double mon2, double mon3, double tong, string chuyenNganh, string ghiChu)
        {
            TuyenSinh entity = new TuyenSinh();
            entity.SBD = sBD;
            entity.BTS = bTS;
            entity.DonVi = donVi;
            entity.MHS = mHS;
            entity.HoTenLot = hoTenLot;
            entity.Ten = ten;
            entity.NgaySinh = ngaySinh;
            entity.GioiTinh = gioiTinh;
            entity.Tinh = tinh;
            entity.Huyen = huyen;
            entity.HoKhau = hoKhau;
            entity.DoiTuong = doiTuong;
            entity.UuTien = uuTien;
            entity.KhuVuc = khuVuc;
            entity.Mon1 = mon1;
            entity.Mon2 = mon2;
            entity.Mon3 = mon3;
            entity.Tong = tong;
            entity.ChuyenNganh = chuyenNganh;
            entity.GhiChu = ghiChu;
            return entity.Update();
        }

        //---------------------------------------------------------------------------------------------

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_Update]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SBD", SqlDbType.Int, SBD);
            db.AddInParameter(dbCommand, "@BTS", SqlDbType.NVarChar, BTS);
            db.AddInParameter(dbCommand, "@DonVi", SqlDbType.NChar, DonVi);
            db.AddInParameter(dbCommand, "@MHS", SqlDbType.NChar, MHS);
            db.AddInParameter(dbCommand, "@HoTenLot", SqlDbType.NVarChar, HoTenLot);
            db.AddInParameter(dbCommand, "@Ten", SqlDbType.NVarChar, Ten);
            db.AddInParameter(dbCommand, "@NgaySinh", SqlDbType.DateTime, NgaySinh.Year < 1753 ? NgaySinh : (object)NgaySinh);
            db.AddInParameter(dbCommand, "@GioiTinh", SqlDbType.Bit, GioiTinh);
            db.AddInParameter(dbCommand, "@Tinh", SqlDbType.NVarChar, Tinh);
            db.AddInParameter(dbCommand, "@Huyen", SqlDbType.NVarChar, Huyen);
            db.AddInParameter(dbCommand, "@HoKhau", SqlDbType.NVarChar, HoKhau);
            db.AddInParameter(dbCommand, "@DoiTuong", SqlDbType.NChar, DoiTuong);
            db.AddInParameter(dbCommand, "@UuTien", SqlDbType.Int, UuTien);
            db.AddInParameter(dbCommand, "@KhuVuc", SqlDbType.NVarChar, KhuVuc);
            db.AddInParameter(dbCommand, "@Mon1", SqlDbType.Float, Mon1);
            db.AddInParameter(dbCommand, "@Mon2", SqlDbType.Float, Mon2);
            db.AddInParameter(dbCommand, "@Mon3", SqlDbType.Float, Mon3);
            db.AddInParameter(dbCommand, "@Tong", SqlDbType.Float, Tong);
            db.AddInParameter(dbCommand, "@ChuyenNganh", SqlDbType.NVarChar, ChuyenNganh);
            db.AddInParameter(dbCommand, "@GhiChu", SqlDbType.NVarChar, GhiChu);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------
        public static bool UpdateCollection(IList<TuyenSinh> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (TuyenSinh item in collection)
                        {
                            if (item.Update(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at UpdateCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int DeleteTuyenSinh(int sBD)
        {
            TuyenSinh entity = new TuyenSinh();
            entity.SBD = sBD;

            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            const string spName = "[dbo].[p_CDTM_TCD_TuyenSinh_Delete]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@SBD", SqlDbType.Int, SBD);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static bool DeleteCollection(IList<TuyenSinh> collection)
        {
            bool ret;
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        bool ret01 = true;
                        foreach (TuyenSinh item in collection)
                        {
                            if (item.Delete(transaction) <= 0)
                            {
                                ret01 = false;
                                break;
                            }
                        }
                        if (ret01)
                        {
                            transaction.Commit();
                            ret = true;
                        }
                        else
                        {
                            transaction.Rollback();
                            ret = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        throw new Exception("Error at DeleteCollection method: " + ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }
            return ret;
        }
        #endregion
    }
}